/*****************************************************************************************
* DISTRIBUTING HOUSEHOLD LEVEL INFORMATION TO THE INDIVIDUAL LEVEL                       *
* In this example we will distribute household level information to individuals          *
* in those households. We can do this by merging household level file (such as w_hhresp) *
* with an individual level file (such as w_indresp) within the same wave.                *
*****************************************************************************************/

// change current file location for output data
cd "SET_YOUR_PATH_FOR_OUTPUT/BHPS"

// assign global macro to refer to BHPS & Understanding Society data
// Note: BHPS data is in folders bhps_w1 etc, UKHLS data in ukhls_w1 etc.
global ukhls "SET_YOUR_PATH_FOR_INPUT/UKDA-6614-stata/stata/stata11_se"

clear



*****************
*** UKHLS *******
*****************

// loop over waves, add household data and save wave-specific files

foreach w in a b c d e f g h i j {
	// find the wave number
	local waveno=strpos("abcdefghijklmnopqrstuvwxyz","`w'")

	// open the individual level file
	use "$ukhls/ukhls_w`waveno'/`w'_indresp", clear
	
	// add youth data
	append using "$ukhls/ukhls_w`waveno'/`w'_youth"
	
	// If you want to include ALL variables from this file then type this instead: use "$ukhls/ukhls_w1/a_hhresp", clear

	// merge household level file
	merge m:1 `w'_hidp using "$ukhls/ukhls_w`waveno'/`w'_hhresp", nogen
		
	// If you want to include ALL variables from this file then type this instead: merge 1:m a_hidp using "$ukhls/ukhls_w1/a_indresp"


	
	// drop the merge variable as it is not needed - essential step
	// _merge = 1 means these are cases in a_hhresp but not in a_indresp
	// _merge = 3 means these are cases which are in both a_hhresp and in a_indresp
	*drop _merge
	
	// keep only relevant variables
	
	isvar `w'_hidp pid pidp `w'_mastat_dv `w'_age_dv `w'_age `w'_mnspid `w'_fnspid `w'_hiqual_dv `w'_jbstat `w'_plbornc /// identifiers and individual specifics
		`w'_hidp `w'_hhsize `w'_fihhmngrs_dv `w'_fihhmnnet1_dv `w'_hhdenus_xw bc_hhyneti bc_hhnyrde bc_hhnyrde2 /// 
		`w'_matdepa `w'_matdepi `w'_matdepj `w'_matdepd `w'_matdepe `w'_matdepf `w'_matdepg `w'_matdeph /// 
		`w'_vote1 `w'_vote2 `w'_vote3 `w'_vote6 `w'_vote7 `w'_voteintent `w'_demorient `w'_perpolinf ///  
		`w'_ypvte3gb `w'_ypvte3ni `w'_ypvte6 `w'_ypfpolitm 	`w'_ypvt11 	/// 
		`w'_yphsw `w'_yphap `w'_yphfm `w'_yphfr `w'_yphsc `w'_yphlf		/// 
		`w'_ypravena11 `w'_ypravenb12 `w'_ypravenc4 `w'_ypravenc12 `w'_ypravend7 `w'_ypravend12 `w'_ypravene1 `w'_ypravene5 `w'_ypravene7 /// 
		`w'_ypsdqcp_dv `w'_ypsdqes_dv `w'_ypsdqha_dv `w'_ypsdqpp_dv `w'_ypsdqps_dv `w'_ypsdqtd_dv		/// 
		`w'_ypesta `w'_ypestb `w'_ypestc `w'_ypeste `w'_ypestf `w'_ypesti `w'_ypestj		/// 
		`w'_ypsdqb `w'_ypsdqc `w'_ypsdqa `w'_ypsdqd `w'_ypsdqe `w'_ypsdqf `w'_ypsdqh `w'_ypsdqm `w'_ypsdqp `w'_ypsdqx		/// 
		`w'_ypargf `w'_ypargm `w'_ypdisbuk `w'_ypfamsup  `w'_yptlkf `w'_yptlkm 		/// 
		`w'_yppareve `w'_yppasc `w'_ypopsc `w'_ypparsch `w'_ypfadmus `w'_ypfadspt `w'_ypfadttr yphmwkhlp		/// 
		`w'_polcost `w'_perbfts `w'_polcost `w'_grpbfts `w'_poleff? `w'_civicduty `w'_votenorm  ///
		`w'_paygu_dv `w'_finnow `w'_finfut `w'_sclfsat2 `w'_fimnnet_dv `w'_fiyr ///   
		`w'_jbsec `w'_depenth1 `w'_depenth2 `w'_depenth3 `w'_depenth4 `w'_depenth5 `w'_depenth6 `w'_julk4wk ///
		`w'_scsf4a `w'_scsf4b `w'_scsf7 `w'_sclfsato `w'_sclfsat1 `w'_sf1 `w'_scsf1 `w'_scghq1_dv `w'_sf12mcs_dv /// (
		`w'_scghqa `w'_scghqb `w'_scghqc `w'_scghqd `w'_scghqe `w'_scghqf `w'_scghqg `w'_scghqh `w'_scghqi `w'_scghqj `w'_scghqk `w'_scghql	// 
	
	local OKlist "`r(varlist)'"
	keep `OKlist'

	// save the file that was created
	drop if pidp==.
	save "./UKHLS/`w'_indresp_hh.dta", replace

		
}



*****************
*** BHPS *******
*****************
clear

// loop over waves, add household data and save wave-specific files

foreach w in a b c d e f g h i j k l m n o p q r {
	// find the wave number
	local waveno=strpos("abcdefghijklmnopqrstuvwxyz","`w'")

	// open the the individual level file
	use "$ukhls/bhps_w`waveno'/b`w'_indresp", clear
	// add youth data
	capture append using "$ukhls/bhps_w`waveno'/b`w'_youth"
	
	// If you want to include ALL variables from this file then type this instead: use "$ukhls/ukhls_w1/a_hhresp", clear

	// merge household level file
	merge m:1 b`w'_hidp using "$ukhls/bhps_w`waveno'/b`w'_hhresp", nogen
		
	// If you want to include ALL variables from this file then type this instead: merge 1:m a_hidp using "$ukhls/ukhls_w1/a_indresp"

	

	// drop the merge variable as it is not needed - essential step
	// _merge = 1 means these are cases in a_hhresp but not in a_indresp
	// _merge = 3 means these are cases which are in both a_hhresp and in a_indresp
	*drop _merge
	
	// keep only relevant variables
	
	isvar b`w'_hidp pidp pid b`w'_mastat_dv b`w'_age_dv b`w'_age b`w'_mnpid_bh b`w'_fnpid_bh b`w'_hiqualb_dv b`w'_jbstat b`w'_plbornc /// identifiers and individual specifics
		b`w'_hidp b`w'_hhsize b`w'_fihhmngrs_dv b`w'_fihhmnnet1_dv b`w'_hhdenus_xw /// HH level
		b`w'_vote1* b`w'_vote2* b`w'_vote3* b`w'_vote6* b`w'_vote7* /// vote 
		b`w'_ypvte3	b`w'_ypvte6							/// Youth Questionnaire
		b`w'_yphsw b`w'_yphap b`w'_yphfm b`w'_yphfr b`w'_yphsc b`w'_yphlf		/// Youth Questionnaire: feelings about life, family, school
		b`w'_ypesta b`w'_ypestb b`w'_ypestc b`w'_ypeste b`w'_ypestf b`w'_ypesti b`w'_ypestj		/// Youth Questionnaire: self-esteem
		b`w'_ypargf b`w'_ypargm b`w'_yptlkf b`w'_yptlkm 		/// Youth Questionnaire: family life
		b`w'_yppasc b`w'_ypopsc 		/// Youth Questionnaire: school and edu
		b`w'_paygu_dv b`w'_finnow* b`w'_sclfsat2* b`w'_fimnnet_dv* b`w'_lfsat2 b`w'_fiyr /// finances  
		b`w'_lfsato b`w'_lfsat1 b`w'_hlsf5a b`w'_hlsf5b b`w'_hlsf5c b`w'_sf36mh   b`w'_scsf4a* b`w'_scsf4b* b`w'_scsf7* b`w'_sclfsat1* b`w'_sf1* b`w'_scsf1* b`w'_scghq1_dv* b`w'_sf12mcs_dv* /// (mental) health 
		b`w'_scghqa* `bw'_scghqb* b`w'_scghqc* b`w'_scghqd* b`w'_scghqe* b`w'_scghqf* b`w'_scghqg* b`w'_scghqh* b`w'_scghqi* b`w'_scghqj* b`w'_scghqk* b`w'_scghql*	// GHQ: decision-making, confidence ...)	
	
	local OKlist "`r(varlist)'"
	keep `OKlist'

	// save the file that was created
	drop if pid==.
	save "./BHPS/b`w'_indresp_hh.dta", replace

	
	
}







